﻿declare @date_update nvarchar(100)= (select DATE_UPDATE from [DATE_EM_REPORT])
declare @month nvarchar(6) = (select MAX_MONTH from [DATE_EM_REPORT])
exec('
delete from TBL_CM_POS

insert TBL_CM_POS
SELECT BUSINESS_DATE='''+@date_update+''',CIF,0 AS GIAO_DICH_IN_MONTH, COUNT(CIF) AS COUNT_TK --INTO TBL_CM_POS
FROM HUONGTT19.POS.DBO.POS_ALL_'+@month+'
WHERE [TRẠNG THÁI TRIỂN KHAI] IN(1,2)
AND CIF IS NOT NULL
GROUP BY CIF')
update TBL_CM_POS
set GIAO_DICH_IN_MONTH = 0 where GIAO_DICH_IN_MONTH is null

--select * from TBL_CM_POS
--select * from HUONGTT19.POS.DBO.POS_ALL_201612